package com.atguigu.study.base.rwlock;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @Author 快点干早点散
 * @Create 2025/4/25 21:03
 * Description:
 */
public class ReadWriteLockDemo2 {
    private ReadWriteLock lock = new ReentrantReadWriteLock();

    public void test(String key,String value) {
        lock.writeLock().lock();
        System.out.println(Thread.currentThread().getName()+"获取写锁== 写入代码");
        System.out.println(Thread.currentThread().getName()+"释放写锁");

        lock.readLock().lock();
        System.out.println(Thread.currentThread().getName()+"获取读锁 ===正在读取");
        lock.writeLock().unlock();


        lock.readLock().unlock();
        System.out.println("释放读锁");


    }

    public static void main(String[] args) {
        ReadWriteLockDemo2 readWriteLockDemo2 = new ReadWriteLockDemo2();
        for (int i = 1; i <= 2; i++) {
            int finalI=i;
            new Thread(() -> {

                readWriteLockDemo2.test(finalI+"",finalI+"");

            }, String.valueOf(i)).start();
        }


    }
}
